package com.common.libhttp;

import android.content.Context;
import android.util.Log;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;

import java.io.File;


public class DownLoaderManager {
    private static DownLoaderManager manager;
    private DownLoadLoadCallback callback;

    public void setCallback(DownLoadLoadCallback callback) {
        this.callback = callback;
    }

    public static DownLoaderManager getManager(Context context) {
        synchronized (DownLoaderManager.class){
            if(manager == null) {
                if (manager == null) {
                    manager = new DownLoaderManager(context.getApplicationContext());
                }
            }
        }

        return manager;
    }

    //私有构造函数，不允许直接创建
    private DownLoaderManager(Context context) {
        FileDownloader.setup(context);
//        File file = new File(fileDirPath);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
    }

    public void downLoadFile(String fileDirPath, String fileName, String url, int tag) {
        File file = new File(fileDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileDownloader.getImpl().create(url)
                .setPath(fileDirPath + fileName)
                .setListener(createListener())
                .setTag(tag)
                .start();
    }

    public void getStatus(String url) {
        FileDownloader.getImpl().create(url).getFilename();
    }


    private FileDownloadListener createListener() {
        return new FileDownloadListener() {

            @Override
            protected boolean isInvalid() {
                Log.d("FileDownloadListener", "isInvalid");
                return false;
            }

            @Override
            protected void pending(final BaseDownloadTask task, final int soFarBytes, final int totalBytes) {
                Log.d("FileDownloadListener", "pending");
            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
                super.connected(task, etag, isContinue, soFarBytes, totalBytes);
                Log.d("FileDownloadListener", "connected");
                if (callback != null) {
                    callback.connect((Integer) task.getTag());
                }
            }

            @Override
            protected void progress(final BaseDownloadTask task, final int soFarBytes, final int totalBytes) {
                Log.d("FileDownloadListener", "progress");
                if (callback != null) {
                    callback.progress(soFarBytes, totalBytes, (int) task.getTag());
                }
            }

            @Override
            protected void blockComplete(final BaseDownloadTask task) {
                Log.d("FileDownloadListener", "blockComplete");
            }

            @Override
            protected void retry(BaseDownloadTask task, Throwable ex, int retryingTimes, int soFarBytes) {
                super.retry(task, ex, retryingTimes, soFarBytes);
                Log.d("FileDownloadListener", "retry");

            }

            @Override
            protected void completed(BaseDownloadTask task) {
                Log.d("FileDownloadListener", "completed");
                if (callback != null) {
                    callback.completed((int) task.getTag());
                }
            }

            @Override
            protected void paused(final BaseDownloadTask task, final int soFarBytes, final int totalBytes) {
                Log.d("FileDownloadListener", "paused");
            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                Log.d("FileDownloadListener", "error");
                if (callback != null) {
                    callback.error((int) task.getTag());
                }
            }

            @Override
            protected void warn(BaseDownloadTask task) {
                Log.d("FileDownloadListener", "warn");
            }
        };
    }


    public interface DownLoadLoadCallback {
        void connect(int tag);

        void progress(int soFarBytes, int totalBytes, int tag);

        void completed(int tag);

        void error(int tag);
    }

}
